home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / find.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-30  |  7.3 KB  |  273 lines

  1. /*
  2.     GWAda Development Environment for 386/486 PCs   
  3.     Copyright (C) 1993, Arthur Vargas Lopes  & Michael Bliss Feldman
  4.                         vlopes@vortex.ufrgs.br mfeldman@seas.gwu.edu
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; version 2 of the License.    
  9.  
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20.  
  21. /*  find.c  */
  22.  
  23. #include <externs.h>
  24.  
  25.  
  26.  
  27. void AVL_PROCESS_FIND()
  28. {
  29.     AVL_FIND(1,"");
  30. }
  31.  
  32. void AVL_PROCESS_REPLACE()
  33. {
  34.     AVL_REPLACE(1);
  35. }
  36.  
  37.  
  38.  
  39. void AVL_FIND(int no,char *what)
  40. {
  41.     char f[AVL_MAX_LINEL+10];
  42.     AVL_EDIT_WINDOW_PTR w;
  43.     AVL_LINE_PTR temp, head = NULL;
  44.     struct rccoord old;
  45.     AVL_WIN_PTR m1 = NULL, m2;
  46.     int n1, n2, x, i, j, k, l, rp;
  47.     static char *msg = " GWAda - Find? ";
  48.     char msg2[80];
  49.     w = &avl_windows[avl_window];
  50.     old = _gettextposition();
  51.     n1 = 62;
  52.     n2 = (80 - n1) / 2;
  53.     if (strlen(what) == 0) {
  54.         m1 = AVL_MAKE_WINDOW(msg,7,n2,9,n1+n2,avl_wnd_bk_color,avl_wnd_color);
  55.         _settextposition(1,1);
  56.         rp = AVL_PROMPT(1,1,avl_find_txt,60);
  57.         if (rp) {
  58.             AVL_DEL_WINDOW(m1);
  59.             return;
  60.             }
  61.         }
  62.     else
  63.         strcpy(avl_find_txt,what);
  64.     temp = w -> current_line;
  65.     i = w -> txt_col;
  66.     while (temp != w -> head)  {
  67.         x = strlen(temp -> line) - (l = strlen(avl_find_txt));
  68.         for(j = i; j < x; ++j)  {
  69.             for(k = 0; k < l; ++k)
  70.                 if (toupper(avl_find_txt[k]) != toupper(temp -> line[j + k]))
  71.                     break;
  72.             if (k == l) { /* Found string here */
  73.                 w -> txt_col = j;
  74.                 w -> current_line = temp;
  75.                 if (strlen(what)) 
  76.                     if (strcmp(what,"$"))  {
  77.                         w -> current_line = w -> current_line -> previous;
  78.                         w -> current_line = w -> current_line -> previous;
  79.                         }
  80.                 w -> scr_row = 1;
  81.                 AVL_DEL_WINDOW(m1);
  82.                 _settextposition(1,1);
  83.                 AVL_UPDATE_SCREEN();
  84.                 return;
  85.                 }
  86.             }
  87.         temp = temp -> next;
  88.         }
  89.     if (!strlen(what))  {
  90.         sprintf(f,"Can't find: %s",avl_find_txt);
  91.         AVL_ERROR(f);
  92.         }
  93.     AVL_DEL_WINDOW(m1);
  94. }
  95.  
  96. void AVL_DO_REPLACE(int from, AVL_LINE_PTR w)
  97. {
  98.     int i, j, k, n, n2, min;
  99.     j = strlen(avl_find_txt);
  100.     k = strlen(avl_replace_txt);
  101.     n2 = strlen(w -> line);
  102.     min = (j > k) ? k : j;
  103.     for (i = 0; i < min; ++i)
  104.         w -> line[i+from] = avl_replace_txt[i];
  105.     if (j == k) return;
  106.     if (j > k) { /* Have to compress the line */
  107.         n = j - k; 
  108.         for(i = from + min; i <= (n2 - n); ++i)
  109.             w -> line[i] = w -> line[i+n];
  110.         }
  111.     else { /* Have to insert remaining replacement */ 
  112.         n = k - j;
  113.         /*  First shift n positions to the right */
  114.         for(i = n2 + n; i >= (from + min); --i)
  115.             w -> line[i] = w -> line[i-n];
  116.         j = min;
  117.         for(i = from + min; j < k; ++i)
  118.             w -> line[i] = avl_replace_txt[j++];
  119.         }
  120. }        
  121.  
  122. void AVL_REPLACE(int no)
  123. {
  124.     char f[AVL_MAX_LINEL+10];
  125.     char f2[AVL_MAX_LINEL+10];
  126.     char f3[AVL_MAX_LINEL+10];
  127.     char f3x[AVL_MAX_LINEL+10];
  128.     char rmsg[180];
  129.     char c;
  130.     AVL_EDIT_WINDOW_PTR w;
  131.     AVL_LINE_PTR temp, last_change, save_line;
  132.     struct rccoord old;
  133.     AVL_WIN_PTR m1, m2, hw2;
  134.     short n1, n2, x, i, j, k, l, question = 0;
  135.     short done = 0, rp, save_txt, save_row, jk;
  136.     static char *msg = " GWAda - Replace what? ";
  137.     static char *msg2 = " GWAda - Replace by? ";
  138.     if (no <= 0) no = 1;
  139.     w = &avl_windows[avl_window];
  140.     old = _gettextposition();
  141.     n1 = 62;
  142.     n2 = (80 - n1) / 2;
  143.     m1 = AVL_MAKE_WINDOW(msg,6,n2,8,n1+n2,avl_wnd_bk_color,avl_wnd_color);
  144.     _settextposition(1,1);
  145.     rp = AVL_PROMPT(1,1,avl_find_txt,60);
  146.     if (avl_find_txt[0] == '\0' || rp)  {
  147.         AVL_DEL_WINDOW(m1);
  148.         _settextposition(old.row,old.col);
  149.         return;
  150.         }
  151.     hw2 = AVL_MAKE_WINDOW(" Examples ",17,39,17+6,39+36,avl_wnd_bk_color,avl_wnd_color);
  152.     _outtext(" Enter:\n");
  153.     _outtext("    All --- replace all occurences\n");
  154.     _outtext("    3   --- replaces up to 3 times\n");
  155.     _outtext("    3?  --- replaces up to 3 times\n");
  156.     _outtext("            asking confirmation");
  157.     m2 = AVL_MAKE_WINDOW(msg2,10,n2,14,n1+n2,avl_wnd_bk_color,avl_wnd_color);
  158.     _settextposition(1,1);
  159.     rp = AVL_PROMPT(1,1,avl_replace_txt,60);
  160.     if (rp)  {
  161.         AVL_DEL_WINDOW(m2);
  162.         AVL_DEL_WINDOW(hw2);
  163.         AVL_DEL_WINDOW(m1);
  164.         _settextposition(old.row,old.col);
  165.         return;
  166.         }
  167.     _settextposition(3,2);
  168.     _outtext("Replace how many times? ");
  169.     strcpy(f3,"1");
  170.     rp = AVL_PROMPT(3,26,f3,6);
  171.     if (rp)  
  172.         f3[0] = '\0';
  173.     else {
  174.         strcpy(f3x,f3);
  175.         for(i = 0; i < strlen(f3); ++i)
  176.             if (f3[i] == '?')  {
  177.                 f3[i] = '\0';
  178.                 question = 1;
  179.                 break;
  180.                 }
  181.         }
  182.     if ((f3[0] == 'A' || f3[0] == 'a') &&
  183.         (f3[1] == 'L' || f3[1] == 'l') &&
  184.         (f3[2] == 'L' || f3[2] == 'l') && f3[3] == '\0') 
  185.         strcpy(f3,"5000");
  186.     AVL_DEL_WINDOW(m2);
  187.     AVL_DEL_WINDOW(hw2);
  188.     AVL_DEL_WINDOW(m1);
  189.     no = atoi(f3);
  190.     if (f3[0] == '\0' || no == 0)  {
  191.         _settextposition(old.row,old.col);
  192.         return;
  193.         }
  194.     save_line = w -> current_line;
  195.     save_txt = w -> txt_col;
  196.     save_row = w -> scr_row;
  197.     temp = w -> current_line;
  198.     i = w -> txt_col;
  199.     while (temp != w -> head)  {
  200.         x = strlen(temp -> line) - (l = strlen(avl_find_txt));
  201. /* was        for(j = i; j < x; ++j)  {  */
  202.         for(j = i; j <= x; ++j)  {
  203.             for(k = 0; k < l; ++k)
  204.                 if (toupper(avl_find_txt[k]) != toupper(temp -> line[j + k]))
  205.                     break;
  206.             if (k == l) { /* Found string here */
  207.                 if (question)  {
  208.                     w -> txt_col = j;
  209.                     w -> current_line = temp;
  210.                     w -> scr_row = 1;
  211.                     AVL_UPDATE_SCREEN();
  212.                     AVL_UPDATE_CURSOR();
  213.                     AVL_UPDATE_STATUS_LINE();
  214.                     for(jk = w -> scr_col; jk < (w -> scr_col + l) && jk < 80 ; ++jk)  {
  215.                         c = *AVL_MAP(2,jk);
  216.                         AVL_WVIDEO(c,(unsigned char) 
  217.                             ((avl_men_bk_color + 16) << 4 | avl_men_letter)
  218.                               , AVL_MAP(2,jk));
  219.                         }
  220.                     _settextposition(1,2);
  221.                     sprintf(rmsg,"Replace (Y/N) ? ", avl_replace_txt);
  222.                     m1 = AVL_MAKE_WINDOW(msg,6,n2,8,n1+n2,avl_wnd_bk_color,avl_wnd_color);
  223.                     _settextposition(1,1);
  224.                     _outtext(avl_find_txt);
  225.                     m2 = AVL_MAKE_WINDOW(msg2,10,n2,14,n1+n2,avl_wnd_bk_color,avl_wnd_color);
  226.                     _settextposition(1,1);
  227.                     _outtext(avl_replace_txt);
  228.                     _settextposition(3,2);
  229.                     _outtext("Replace how many times? ");
  230.                     _outtext(f3x);
  231.                     if (AVL_QUESTION(rmsg) != 'Y')  {
  232.                         AVL_DEL_WINDOW(m2);
  233.                         AVL_DEL_WINDOW(m1);
  234.                         continue;
  235.                         }
  236.                     AVL_DEL_WINDOW(m2);
  237.                     AVL_DEL_WINDOW(m1);
  238.                     }
  239.                 w -> changed = 1;
  240.                 AVL_DO_REPLACE(j,temp);
  241.                 j += (strlen(avl_replace_txt) - 1);
  242.                 ++done;
  243.                 last_change = temp;
  244.                 if (--no == 0)  goto job_done;
  245.                 }
  246.             }
  247.         temp = temp -> next;
  248.         i = 0;
  249.         }
  250.  
  251. job_done:
  252.  
  253.     if (question && done)  {    
  254.         w -> current_line = save_line;
  255.         w -> txt_col = save_txt;
  256.         w -> scr_row = save_row;
  257.         }
  258.     if (!done)  {
  259.         sprintf(f,"Can't replace: %s",avl_find_txt);
  260.         AVL_ERROR(f);
  261.         }
  262.     else  {
  263.         w -> txt_col = j;
  264.         w -> current_line = last_change;
  265.         w -> scr_row = 1;
  266.         _settextposition(old.row,old.col);
  267.         AVL_UPDATE_SCREEN();
  268.         sprintf(f,"Replaced %s %d time%s",avl_find_txt,done, (done > 1) ? "s" : "");
  269.         AVL_TELL(f);
  270.         }
  271.     _settextposition(old.row,old.col);
  272. }
  273.  
  274.